//
//  ArraneFiveView.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ArraneFiveView.h"
#import <QuartzCore/QuartzCore.h>

@interface ArraneFiveView (Private)

-(void)setDefault;

@end

@implementation ArraneFiveView

@synthesize currentType=_currentType;

-(void)dealloc{

    [bellSelectContentView release];
    [pageControl release];
    [topSelectViewArray release];
    [titleLabel release];
    [super dealloc];
}

-(id)initWithCoder:(NSCoder *)aDecoder{

    self=[super initWithCoder:aDecoder];
    if (self) {
        [self setDefault];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self setDefault];
        
    }
    return self;
}

-(void)setDefault{

    _currentType=-1;
    
    self.layer.cornerRadius=6.0f;
    self.layer.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4].CGColor;
    
    topSelectViewArray=[NSMutableArray new];
    
    NSArray *tempTitleArray=[NSArray arrayWithObjects:@"万",@"千",@"百",@"十",@"个", nil];
    
    titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10.0f, 5.0f+(tempTitleArray.count*(5.0f+40.0f)),self.frame.size.width-20.0f, 18.0f)];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.text=@"Test title text";
    titleLabel.font=[UIFont systemFontOfSize:14.0f];
    titleLabel.textAlignment=UITextAlignmentCenter;
    [self addSubview:titleLabel];
    
    bellSelectContentView=[[UIScrollView alloc]initWithFrame:CGRectMake(10.0f, titleLabel.frame.origin.y+18.0f+5.0f,self.frame.size.width-20.0f, 80.0f)];
    bellSelectContentView.pagingEnabled=YES;
    bellSelectContentView.backgroundColor=[UIColor clearColor];
    bellSelectContentView.contentSize=CGSizeMake(bellSelectContentView.frame.size.width*tempTitleArray.count, 80.0f);
    bellSelectContentView.showsVerticalScrollIndicator=NO;
    bellSelectContentView.showsHorizontalScrollIndicator=NO;
    [self addSubview:bellSelectContentView];
    bellSelectContentView.delegate=self;
    
    for (int i=0; i<tempTitleArray.count; i++) {
        NSString *tempTitleWord=[tempTitleArray objectAtIndex:i];
        NSString *tempPlaceString=[NSString stringWithFormat:@"点击这里选择%@位数",tempTitleWord];
        ThreeDSelectView *tempSelectView=[[ThreeDSelectView alloc]initWithFrame:CGRectMake(10.0f, 5.0f+i*(5.0f+40.0f),self.frame.size.width-20.0f, 40.0f)];
        [self addSubview:tempSelectView];
        [tempSelectView addTarget:self action:@selector(selectViewIsDone:) forControlEvents:UIControlEventTouchUpInside];
        [topSelectViewArray addObject:tempSelectView];
        tempSelectView.tag=i;
        tempSelectView.placeholder=tempPlaceString;
        tempSelectView.bitTitle=tempTitleWord;
        [tempSelectView release];
        
        ArraneBellSelectView *tempBellSelectView=[[ArraneBellSelectView alloc]initWithFrame:CGRectMake(i*bellSelectContentView.frame.size.width, 0.0f, bellSelectContentView.frame.size.width, bellSelectContentView.frame.size.height)];
        tempBellSelectView.selectView=tempSelectView;
        tempSelectView.selectView=tempBellSelectView;
        [bellSelectContentView addSubview:tempBellSelectView];
        [tempBellSelectView release];
    }
    
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0.0f, self.frame.size.height-15.0f, self.frame.size.width, 15.0f)];
    [self addSubview:pageControl];
    pageControl.backgroundColor=[UIColor clearColor];
    pageControl.numberOfPages=5;
    
    self.currentType=0;
}

-(void)setCurrentType:(NSInteger)currentType{

    if (_currentType==currentType) {
        return;
    }
    
    if (_currentType>=0) {
        ThreeDSelectView *lastSelect=[topSelectViewArray objectAtIndex:_currentType];
        lastSelect.isSelected=NO;
    }
    
    ThreeDSelectView *thisSelect=[topSelectViewArray objectAtIndex:currentType];
    thisSelect.isSelected=YES;
    pageControl.currentPage=currentType;
    [bellSelectContentView scrollRectToVisible:CGRectMake(currentType*bellSelectContentView.frame.size.width, 0.0f, bellSelectContentView.frame.size.width, bellSelectContentView.frame.size.height) animated:YES];
    
    _currentType=currentType;
}

-(void)selectViewIsDone:(ThreeDSelectView *)selectView{

    self.currentType=selectView.tag;
}

#pragma mark -UIScrollViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.currentType=scrollView.contentOffset.x/scrollView.frame.size.width;
}

- (void)randomNumber{

    int i=0;
    while (i<topSelectViewArray.count) {
        
        int index = arc4random() % 9;
        ThreeDSelectView *DSelect=[topSelectViewArray objectAtIndex:i];
        
        [DSelect removeAllNumber];
        
        [DSelect addNumber:index];
        
        [DSelect.selectView removeAllnum];
        [DSelect.selectView randomNumber:index];
        
        i++;
    }
    
}

@end

@implementation ArraneBellSelectView

-(id)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
    if (self) {
        
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        
        self.backgroundColor=[UIColor clearColor];
        float margin=(frame.size.width-40.0f-5*30.0f)/4;
        
        for (int i=0; i<10; i++) {
            NSInteger lines=i/5;
            NSInteger count=i-lines*5;
            
            BellButton *btn=[BellButton creatBellWithFrame:CGRectMake(15.0f+count*(30.0f+margin), lines*(30.0f+10.0f), 30.0f, 30.0f)];
            btn.number=i;
            btn.bellColor=BellColorRed;
            btn.tag=i;
            btn.bellState=BellStateNormal;
            [btn addTarget:self action:@selector(bellDone:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview: btn];
        }
    }
    return self;
}

@end
